package com.spa.domain.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.admin.MassagistOnofExportDTO;
import com.spa.application.dto.admin.MassagistOnoffDTO;
import com.spa.application.query.admin.MassagitsServiceTimePageQuery;
import com.spa.application.query.admin.MassagitsonoffPageQuery;
import com.spa.domain.entity.MassagistOnoff;
import com.spa.infrastructure.mapper.MassagistMapperStruct;
import com.spa.infrastructure.mapper.MassagistOnoffMapperStruct;
import com.spa.infrastructure.repository.condition.MassagistOnoffCondition;
import com.spa.infrastructure.repository.po.MassagistOnoffPO;
import com.spa.infrastructure.repository.service.MassagistOnoffRepositoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MassagistOnoffService{

    @Autowired
    private MassagistOnoffRepositoryService massagistOnoffRepositoryService;


    /**
     * 根据技师ID获取服务总时间
     */
    public Long sumServiceTimeByMassagistId(Long massagistId, String beginDate, String endDate) {
        MassagistOnoffCondition condition = new MassagistOnoffCondition();
        condition.setMassagistId(massagistId);
        condition.setBeginDate(DateUtil.parse(beginDate));
        condition.setEndDate(DateUtil.parse(endDate));
        List<MassagistOnoffPO> massagistOnoffPOS = massagistOnoffRepositoryService.queryList(condition);
        return massagistOnoffPOS.stream().map(po -> {
            return DateUtil.between(po.getOnline(), ObjectUtil.isNull(po.getOffline())?new Date():po.getOffline(), DateUnit.MINUTE);
        }).reduce(0L, Long::sum);
    }

    public Page<MassagistOnoffPO> page(MassagitsServiceTimePageQuery page) {
        MassagistOnoffCondition condition = MassagistOnoffMapperStruct.INSTANCE.query2condition(page);
        return massagistOnoffRepositoryService.page(condition);
    }

    public PageResult<MassagistOnoffDTO> pageMassagistOnoff(MassagitsonoffPageQuery query) {
        MassagistOnoffCondition condition = MassagistOnoffMapperStruct.INSTANCE.pageQuery2condition(query);
        Page<MassagistOnoffPO> massagistOnoffPOPage = massagistOnoffRepositoryService.pageJoin(condition);
        return MassagistOnoffMapperStruct.INSTANCE.po2OnoffDto4page(massagistOnoffPOPage);
    }

    public void saveOnTime(Long massagistId) {
        MassagistOnoffCondition condition = new MassagistOnoffCondition();
        condition.setMassagistId(massagistId);
        massagistOnoffRepositoryService.deleteNotOff(condition);

        MassagistOnoffPO massagistOnoffPO = new MassagistOnoffPO();
        massagistOnoffPO.setMassagistId(massagistId);
        massagistOnoffPO.setOnline(new Date());
        massagistOnoffRepositoryService.save(massagistOnoffPO);
    }

    public void saveOffTime(Long massagistId) {
        MassagistOnoffCondition condition = new MassagistOnoffCondition();
        condition.setMassagistId(massagistId);
        try {
            List<MassagistOnoffPO> notOff = massagistOnoffRepositoryService.getNotOff(condition);
            if (CollectionUtil.isNotEmpty(notOff)){
                MassagistOnoffPO massagistOnoff =notOff.get(0);
                if (ObjectUtil.isNotNull(massagistOnoff)) {
                    Date offline = new Date();
                    massagistOnoff.setOffline(offline);
                    massagistOnoff.setDuration(DateUtil.between(massagistOnoff.getOnline(), offline, DateUnit.MINUTE));
                    massagistOnoffRepositoryService.updateById(massagistOnoff);
                }
            }
        }catch (Exception e){
            log.error("技师：{} 下线操作工作时限 error：{}",massagistId,e.getMessage());
        }

    }

    /**
     * 获取按摩师开关状态列表。
     * @return 返回一个包含按摩师开关状态的实体列表。
     */
    public List<MassagistOnoff> onoffList(MassagitsonoffPageQuery query) {
        // 从仓库服务获取列表，并通过Mapper结构将其转换为实体列表
        MassagistOnoffCondition condition = MassagistOnoffMapperStruct.INSTANCE.pageQuery2condition(query);
        return MassagistOnoffMapperStruct.INSTANCE.po2Entity4List(massagistOnoffRepositoryService.onffList(condition));
    }
}
